<?php
class MenuDisplay extends Base  {
	private $rightList=array();
	private $rightConfig=array();
	private static $instance=null;
	private $otherRightConfig=array();
	private $isAdmin=false;
	/**
	 * 构造函数
	 *
	 * @param unknown_type $rights
	 */
	public function __construct($rights="",$configs=array()) {
		global $_SESSION;
		if (!is_array($rights) && !empty($rights[0])) {
//			print_r($rights);
			$this->rightList=explode(',',$rights);
		} else {
			$rights=$_SESSION['role_right_detail']; 
			$this->rightList=$rights;
		}
		if($_SESSION['isAdmin']=='Yes') {
			$this->isAdmin=true;
		}
		if (empty($configs)) {
				require(ROOT_PATH."/customConfig/menu_base.config.php");
				$configs = $_CfgBase;
		}
		$this->rightConfig=&$configs;
		if($this->isAdmin) {
			$this->rightList = $this->getAllRights(false);
		}
		$this->otherRightConfig=$this->getOtherRightConfig();
	}
	public static function &getInstance($rigths="",$configs=array()) {
		if (self::$instance==null) {
			self::$instance=new MenuDisplay($rigths,$configs);
		}
		return self::$instance;
	}

	/**
	 * 根据玩家获得的权限
	 *
	 * @param array $userId
	 */
	public function getUserRight($userId) {
		return ($this->rightList);
	}
	public function checkCanPass($code) {
		if (!is_numeric($code) || !in_array($code,$this->rightList)) {
			$isIndex = $_SERVER['REQUEST_URI']==$_SERVER['SCRIPT_NAME'];
 			$otherIndex =$_SERVER['SCRIPT_NAME']."?noRight";
 			$isOtherIndex = $_SERVER['REQUEST_URI']==$otherIndex;
//  			var_dump($isIndex,$isOtherIndex);
			if(!$isIndex) {	
		  	  die('You are not authorized to view this page');
			}
 
		} else {
			if(!$this->checkRequire($code)) {
				die('You are not authorized to view this page');
			}
		}
	}
	public function getUserMenu() {
		$defaultConfig=$this->rightConfig; 
		foreach ($defaultConfig as $dk=>$dv) {
			if (!in_array($dv['num'],$this->rightList) && !$this->isAdmin) {
				unset($defaultConfig[$dk]);
				continue;
			}
			if (isset($dv['subMenu']) && is_array($dv['subMenu'])) {	
				foreach ($dv['subMenu'] as $dvk=>$dvv) {
					if (!in_array($dvv['num'],$this->rightList) && !$this->isAdmin) {
						unset($defaultConfig[$dk][$dvk]);
						continue;
					}
					if (isset($dvv['subMenu']) && is_array($dvv['subMenu'])) {	
						foreach ($dvv['subMenu'] as $dvvk=>$dvvv) {
							if (!in_array($dvvv['num'],$this->rightList) && !$this->isAdmin) {
								unset($defaultConfig[$dk][$dvk][$dvvv]);
								continue;
							}
						}
					}					
				}
			}
		}
		return $defaultConfig;
	}
	public function getConfigs() {
		return $this->rightConfig;
	}
	public function getAllRights($isReturnString=true,$data=array()) {
		$return=array();
		if (empty($data)) {
			$data=$this->rightConfig;
		}
		foreach ($data as $kr=>$rv) {
			$return[]=$rv['num'];
			if (isset($rv['subMenu']) && !empty($rv['subMenu'])) {
				$returnTmp=$this->getAllRights(false,$rv['subMenu']);
				$return=array_merge($return,$returnTmp);
			}
		}
		if ($isReturnString) {
			return implode(',',$return);
		} else {
			return $return;
		}
	}
	public function getOtherRightConfig($data=array()) {
		$return=array();
		if (empty($data)) {
			$data=$this->rightConfig;
		}
		foreach ($data as $kr=>$rv) {

			$uda= $rv;
			unset($uda['subMenu']);
			$return[$rv['num']]=$uda;			
			if (isset($rv['subMenu']) && !empty($rv['subMenu'])) {
				$returnTmp=$this->getOtherRightConfig($rv['subMenu']);
				$return=array_merge($return,$returnTmp);
			}
		} 
		
		return $return;
		 
	}
	/**
	 * 获取添加与删除权限
	 * @param int $num
	 * @param string $operateKey
	 */
	public function getAUDByNum($num,$operateKey="a") {
		$operates = isset($this->otherRightConfig[$num]['operate'])?$this->otherRightConfig[$num]['operate']:array();
		if(in_array($operates, $operateKey)){
			return true;
		}
		return false;
	}
	/**
	 * 验证地址是否合法
	 * @param unknown_type $num
	 * @return boolean
	 */
	public function checkRequire($num) {
		$numInfo = $this->otherRightConfig[$num];
		$params     =isset($numInfo['params'])?$numInfo['params']:array();
		$result=true;
		if(!empty($params)) {
			foreach ($params as $pk=>$pv) {
				if(!isset($_GET[$pk]) || trim($_GET[$pk])!=$pv) {
					$result &=false;
				}
			}
		}
		
		return $result;
		
	}
	
}